/*
 * @Author: wangming
 * @Date: 2022-01-13 16:41:43
 * @LastEditors: wangming
 * @LastEditTime: 2022-01-18 18:30:17
 * @FilePath: /hanma-application-designer-fed/tvs.js
 * @Description: 
 */

const fs = require('fs-extra');
const fg = require('fast-glob');
const path = require('path');
const ROOTPATH = path.resolve('src');
let paths = fg.sync(['!admin', '**.js'], {
    cwd: ROOTPATH + '/apis/',
    onlyFiles: false
})
const pagePaths = fg.sync(
    ['**.vue', '**.js', '!apis', '!api'],
    {
        cwd: ROOTPATH,
        onlyFiles: false
    }
);
pagePaths.forEach((i) => {
    var data = fs.readFileSync(path.join(ROOTPATH, i), 'utf-8');
    let O = {}, index = 0;
    let ALLnumber = data.match(/import\s+\{[\s\w,]+\}\s+from\s+['|"]@\/apis\/admin\S+/g)?.length
    let newdata = data.replace(/import\s+\{[\s\w,]+\}\s+from\s+['|"]@\/apis\/admin\S+/g, (res) => {
        index++
        let funName = res.match(/\{[\s\w,]+\}/g)[0].match(/\w+/g)
        funName.forEach(name => {
            var flag = true
            paths.forEach(url => {
                var data = fs.readFileSync(path.join(ROOTPATH, '/apis/' + url), 'utf-8');
                if (data.indexOf(name + '(') > -1) {
                    flag = false
                    if (!!O[url]) {
                        O[url].push(name)
                    } else {
                        O[url] = [name]
                    }
                }
            })
            if (flag) {
                console.error(name)
            }
        })
        let inp = []
        for (let i in O) {
            inp.push(`import { ${O[i].join(',')} } from '@/apis/${i}'`)
        }
        if (index == ALLnumber) {
            return inp.join(['\n'])
        } else {
            return ''
        }


    })
    fs.writeFileSync(path.join(ROOTPATH, i), newdata);
});